package com.yiwyn.service;

import com.yiwyn.domain.BookSearchDTO;
import com.yiwyn.repository.BookElasticsearchRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class BookService {

    @Autowired
    private BookElasticsearchRepository bookElasticsearchRepository;


    public void save(String name) {
        BookSearchDTO bookSearchDTO = new BookSearchDTO();
        bookSearchDTO.setBookName(name);
        bookSearchDTO.setId(UUID.randomUUID().toString().replace("-", ""));
        bookSearchDTO.setAuthor(name + "author");
        try {
            BookSearchDTO save = bookElasticsearchRepository.save(bookSearchDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public List<BookSearchDTO> search(String name) {
        List<BookSearchDTO> bookSearchDTOSByBookName = bookElasticsearchRepository.findBookSearchDTOSByBookName(name);
        return bookSearchDTOSByBookName;
    }

    public void deleteAll(){
        bookElasticsearchRepository.deleteAll();
    }

}
